<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUserCoursesTable extends Migration
{

    protected $tableName = 'user_courses';

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create($this->tableName, function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('order_id')->comment('订单ID');
            $table->unsignedInteger('user_id')->comment('用户ID');
            $table->unsignedInteger('course_id')->comment('课程ID');
            $table->unsignedTinyInteger('level')->comment('课程等级');

            $table->unsignedTinyInteger('score')->default(0)->comment('评分');
            $table->unsignedTinyInteger('star')->default(0)->comment('评星');

            $table->unsignedTinyInteger('is_end')->default(0)->comment('是否结束，0否，1 推送结束，2 阅读结束');
            $table->unsignedTinyInteger('is_shared')->default(0)->comment('是否分享过，0否，1是, @todo delete');
            $table->unsignedTinyInteger('is_current')->default(0)->comment('是否当前在读，0否，1是');

            $table->timestamp('start_time')->nullable()->comment('开始学习时间');
            $table->timestamp('end_time')->nullable()->comment('结束学习时间');

            $table->unsignedSmallInteger('lesson_count')->default(0)->comment('已学习课时数');
            $table->unsignedMediumInteger('word_count')->default(0)->comment('累计阅读量，用于分享');
            $table->unsignedSmallInteger('dub_count')->default(0)->comment('跟读次数');

            $table->unsignedTinyInteger('status')->default(1)->comment('状态，0不可用，1可用');

            $table->timestamps();
            $table->softDeletes();

            $table->index(['user_id', 'course_id'], 'user_course');
            $table->index('order_id');
        });
        DB::statement("ALTER TABLE `$this->tableName` comment '用户购买周期内统计'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists($this->tableName);
    }
}
